package com.lepu.app.fun.chat.business;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.app.application.MyApplication;
import com.app.utils.SdLocal;
import com.app.utils.Setting;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.fun.chat.bean.DeviceInfo;
import com.lepu.app.fun.chat.business.chatcore.ChatManager;
import com.lepu.app.usercenter.bean.LoginInfo;


public class AppCommonService {

    private AppCommonService() {
    }

    static class AppCommonServiceHolder {
        static AppCommonService appCommonService = new AppCommonService();
    }

    public static AppCommonService getInstance() {
        return AppCommonServiceHolder.appCommonService;
    }
//    private static Logger logger = LoggerFactory.getLogger(AppCommonService.class);


    public int getOfflineMsgIQQueryTime() {
        return 5000;
    }

    /**
     * 获得 屏幕的宽高
     *
     * @param activity
     */
    public void getScreenInfo(Activity activity) {
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;  // 屏幕宽度（像素）
        int height = metric.heightPixels;  // 屏幕高度（像素）
        float density = metric.density;  // 屏幕密度（0.75 / 1.0 / 1.5/2.0）
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI（120 / 160 / 240/320）
        int screenWidth = (int) (width / density);//屏幕宽度(dp)
        int screenHeight = (int) (height / density);//屏幕高度(dp)
//        MyApplication.getInstance().setDpHeight(screenHeight);
//        MyApplication.getInstance().setDpWidth(screenWidth);
        Toast.makeText(activity, " width: " + width + " height: " + height
                + " density: " + density
                + " densitydpi " + densityDpi + "screenWidth: "
                + screenWidth + " screenHeight: " + screenHeight, Toast.LENGTH_LONG).show();
        //logger.debug("pixel", "width:" + width + "height:" + height + " density:" + density + " densitydpi" + densityDpi);
    }

    public DeviceInfo getDeviceInfo(Activity activity) {

        DeviceInfo deviceInfo = new DeviceInfo();
        TelephonyManager tm = (TelephonyManager) activity.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        String deviceId = tm.getDeviceId();
        if (deviceId == null || TextUtils.isEmpty(deviceId)) {
            deviceId = "null";
        }
        deviceInfo.setDeviceId(deviceId);
        String osVersion = Build.VERSION.RELEASE;
        if (osVersion == null || TextUtils.isEmpty(osVersion)) {
            osVersion = "null";
        }
        deviceInfo.setOsVersion(osVersion);
        deviceInfo.setResolution(dm.widthPixels + "*" + dm.heightPixels);
        deviceInfo.setDeviceType("Android");
        String networkOperator = tm.getNetworkOperator();
        if (networkOperator == null || TextUtils.isEmpty(networkOperator)) {
            networkOperator = "null";
        }
        deviceInfo.setNetworkOperator(networkOperator);
        try {
            PackageInfo pInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
            deviceInfo.setClientVersion(pInfo.versionName);
        } catch (Exception e) {
            //logger.error(e.toString(), e);
            deviceInfo.setClientVersion("null");
            return deviceInfo;
        }
        return deviceInfo;
    }

    /**
     * 登陆 xmpp
     */
    public void setupChatService() {
        LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
        if (loginInfo == null) {
            return;
        }
        ChatManager.getInstance().login(Setting.CHAT_SERVICE_HOST, Setting.CHAT_SERVICE_PORT,
                Long.valueOf(loginInfo.UserID),
                loginInfo.LoginToken);

    }

    /**
     * 得到应用程序根目录
     *
     * @return
     */
    public String getAppRootStorage() {
        return SdLocal.getLepuRootPath(MyApplication.getInstance().getApplicationContext());
    }

    /**
     * 退出xmpp
     */
    public void logoutXGXMPP() {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    ChatManager.getInstance().logout();
                    LogUtilBase.LogD("xmpp appcommonService", "exit success ");
                } catch (Exception ex) {
                    Log.e("AppCommonService", "XMPP", ex);
                }
            }
        };
        new Thread(run).start();
    }

}
